package cn.iocoder.yudao.module.system.service.productionplandetail;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.system.controller.admin.productionplandetail.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.productionplandetail.ProductionPlanDetailDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.system.dal.mysql.productionplandetail.ProductionPlanDetailMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;

/**
 * 生产计划详情 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class ProductionPlanDetailServiceImpl implements ProductionPlanDetailService {

    @Resource
    private ProductionPlanDetailMapper productionPlanDetailMapper;

    @Override
    public Integer createProductionPlanDetail(ProductionPlanDetailSaveReqVO createReqVO) {
        // 插入
        ProductionPlanDetailDO productionPlanDetail = BeanUtils.toBean(createReqVO, ProductionPlanDetailDO.class);
        productionPlanDetailMapper.insert(productionPlanDetail);
        // 返回
        return productionPlanDetail.getId();
    }

    @Override
    public void updateProductionPlanDetail(ProductionPlanDetailSaveReqVO updateReqVO) {
        // 校验存在
        validateProductionPlanDetailExists(updateReqVO.getId());
        // 更新
        ProductionPlanDetailDO updateObj = BeanUtils.toBean(updateReqVO, ProductionPlanDetailDO.class);
        productionPlanDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteProductionPlanDetail(Integer id) {
        // 校验存在
        validateProductionPlanDetailExists(id);
        // 删除
        productionPlanDetailMapper.deleteById(id);
    }

    @Override
        public void deleteProductionPlanDetailListByIds(List<Integer> ids) {
        // 校验存在
        validateProductionPlanDetailExists(ids);
        // 删除
        productionPlanDetailMapper.deleteByIds(ids);
        }

    private void validateProductionPlanDetailExists(List<Integer> ids) {
        List<ProductionPlanDetailDO> list = productionPlanDetailMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(PRODUCTION_PLAN_DETAIL_NOT_EXISTS);
        }
    }

    private void validateProductionPlanDetailExists(Integer id) {
        if (productionPlanDetailMapper.selectById(id) == null) {
            throw exception(PRODUCTION_PLAN_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public ProductionPlanDetailDO getProductionPlanDetail(Integer id) {
        return productionPlanDetailMapper.selectById(id);
    }

    @Override
    public PageResult<ProductionPlanDetailDO> getProductionPlanDetailPage(ProductionPlanDetailPageReqVO pageReqVO) {
        return productionPlanDetailMapper.selectPage(pageReqVO);
    }

}
